<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>模仿输入框</title>

    <style>
        #search.placeholder {
            color: #999;
        }

        #search {
            width: 200px;
            color: #000;
            padding: 8px;
            border: 1px solid #ccc;
            border-radius: 4px;
        }

        .count {
            font-size: 12px;
            color: #666;
            margin-top: 5px;
        }
    </style>

</head>

<body>
    <input type="text" id="search" class="placeholder" autocomplete="off" value="搜索...">
    <div class="count">还可以输入15个字</div>
</body>
<script>
    let input = document.querySelector('#search');
    let count = document.querySelector('.count');
    let placeholderText = '搜索...';
    let maxLength = 15;

    input.addEventListener('focus', function () {
        if (this.value == placeholderText) {
            this.value = '';
            this.className = '';
        }
    });

    input.addEventListener('blur', function () {
        if (!this.value.trim()) {
            this.value = placeholderText;
            this.className = 'placeholder';
        }
    });

    input.addEventListener('input', function () {
        let len = this.value.length;
        if (len > maxLength) {
            this.value = this.value.substring(0, maxLength);
            len = maxLength;
        }
        let remainLen = maxLength - len;
        count.innerHTML = `还可以输入${remainLen}个字`;
    })

</script>

</html>